<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 链表，动态的数组
    // 链表不是连续性的
    // 适合练习递归
    /*
    * 包含 data/element 和 next (单纯指向下一个对象)
    */
   class Node{
     constructor(element) {
       this.element = element
       this.next = null
     }
   }
   class LinkNodeList{
     constructor() {
      //  链表
      // 链表相关的操作
      this.head = null
      this.length = 0
     }
    //  增删改查
     append(ele) {
       let node = new Node(ele)
       let cur
       if(this.head == null) {
         this.head = node
       }else{
         cur = this.head
         while(cur.next) {
          cur = cur.next
         }
         cur.next = node
       }
       this.length += 1
     }

     print() {
       let arr = []
       let cur = this.head
      //  if(cur) {
      //    arr.push(cur.element)
      //    cur = cur.next
      //    while(cur) {
      //     arr.push(cur.element)
      //    }
      //    return arr.join('==>')
      //  }else{
      //    return ''
      //  }
      while(cur) {
        arr.push(cur.element)
        cur = cur.next
      }
      return arr.join('==>')
     }

     removeAt(index) {
      // 找到这个元素
      // 边界情况
      let cur = this.head
      let prev
      let i = 0
      if(index <= 0) {
        this.head = cur.next
      }else{
        while(i < index && i<this.length-1) {
          prev = cur
          cur = cur.next
          i++
        }
        // console.log(cur)
        prev.next = cur.next
        cur.next = null
      }
      this.length -= 1
      return cur.element
     }
   }
   let linknodelist = new LinkNodeList()
   linknodelist.append('你')
   linknodelist.append('好')
   linknodelist.append('呀')
   linknodelist.append('！')
   console.log(linknodelist.print())
  //  删除
   linknodelist.removeAt(6)
   console.log(linknodelist.print())
  //  翻转链表
  function reverse(head) {

    if(linknodelist.length <= 1) {
      return head
    }else{
      let pre = {}
      let cur = head
      while (cur) {
        pre.next = cur
        cur.next = null
      }

    }
  }

  </script>
</body>
</html>